<template>
  <!-- 岗位管理编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
    <el-form ref="postFormRef" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="岗位编码" prop="postCode">
        <el-input v-model="form.postCode" placeholder="请输入岗位编码" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="form.postName" placeholder="请输入岗位名称" />
      </el-form-item>
      <el-form-item label="岗位排序" prop="postSort">
        <el-input-number v-model="form.postSort" :min="1" label="请输入岗位排序" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_normal_disable"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark" class="item-describe">
        <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts" name="PostEdit">

import { addPostApi, getPostApi, updatePostApi } from "@/api/system/post";

const { proxy } = getCurrentInstance();

const { sys_normal_disable } = useDict("sys_normal_disable")

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    default: null,
  },
  title: {
    type: String,
    default: false,
  }
})

// 岗位管理表单
const postFormRef = ref(null);

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<PostRecord>({});

// 表单校验
const rules = ref({
  postCode: [
    { required: true, message: "岗位编码不能为空", trigger: "blur" },
  ],
  postName: [
    { required: true, message: "岗位名称不能为空", trigger: "blur" },
  ],
  postSort: [
    { required: true, message: "岗位排序不能为空", trigger: "blur" },
  ],
  status: [
    { required: true, message: "状态不能为空", trigger: "blur" },
  ],
});

// 加载
const loading = ref(false);

/** 获取岗位管理详情 */
function getData() {
  reset();
  if (props.id || props.id === 0) {
    getPostApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(() => {
      closeDialog(false);
    })
  } else {
    open.value = true;
  }
}

/** 表单重置 */
function reset() {
  form.value = {
    postId: null,
    postCode: null,
    postName: null,
    postSort: null,
    status: SYS_NORMAL_DISABLE_ENUM.normal,
    createTime: null,
    remark: null,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  postFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (form.value.postId) {
        updatePostApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false
        });
      } else {
        addPostApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false
        });
      }
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
})

getData();
</script>
<style lang="scss" scoped>

</style>
  